/********************************************************/
/*	Author:		gong_libin			*/
/*	Date:		2017_11_01			*/
/*	File:		CmProJsn.cpp			*/
/********************************************************/

#include "CmProJsn.h"

int CCmProJsn::CmProtoSerialize(CMADXPARA_S* pstPara)
{
	memcpy(&pstPara->m_stDspReq, &pstPara->m_stSspReq, sizeof(CMADXMSG_S));

	return CM_SUCCESS;
}

int CCmProJsn::CmProtoDeserialize(CMADXPARA_S* pstPara)
{
	memcpy(&pstPara->m_stSspRes, &pstPara->m_stDspRes, sizeof(CMADXMSG_S));

	return CM_SUCCESS;
}

CMADXINFO_S* CCmProJsn::CmProtoValidity(char* pszPacket)
{
	CMADXINFO_S* pstInfo = NULL;

	if (NULL != (pstInfo = CmProtoDeserialize(pszPacket))) {
		pstInfo = pstInfo->m_uiMinPrice < pstInfo->m_uiMaxPrice && CMSTR_C::npos != pstInfo->m_CCats.find(pstInfo->m_CCategory) ? pstInfo : NULL;
	}

	return pstInfo;
}

CMSTR_C CCmProJsn::CmProtoSerialize(CMADXINFO_S* pstAdInfo)
{
	Json::Value CJsn;
	const char* pszReq[] = CM_ADF_TREQ;
	const char** ppszReq = &pszReq[0];

	CJsn[*ppszReq ++] = pstAdInfo->m_uiVer;
	CJsn[*ppszReq ++] = pstAdInfo->m_uiDsp;
	CJsn[*ppszReq ++] = pstAdInfo->m_CCtxtid;
	CJsn[*ppszReq ++] = pstAdInfo->m_CIP;
	CJsn[*ppszReq ++] = pstAdInfo->m_CUid;
	CJsn[*ppszReq ++] = pstAdInfo->m_CMac;
	CJsn[*ppszReq ++] = pstAdInfo->m_CGps;
	CJsn[*ppszReq ++] = pstAdInfo->m_CImei;
	CJsn[*ppszReq ++] = pstAdInfo->m_CUa;
	CJsn[*ppszReq ++] = pstAdInfo->m_CDid;
	CJsn[*ppszReq ++] = pstAdInfo->m_CAid;
	CJsn[*ppszReq ++] = pstAdInfo->m_CEid;
	CJsn[*ppszReq ++] = pstAdInfo->m_CCid;
	CJsn[*ppszReq ++] = pstAdInfo->m_CCats;
	CJsn[*ppszReq ++] = pstAdInfo->m_CType;
	CJsn[*ppszReq ++] = pstAdInfo->m_CWidth;
	CJsn[*ppszReq ++] = pstAdInfo->m_CHeight;
	CJsn[*ppszReq ++] = pstAdInfo->m_CScaling;
	CJsn[*ppszReq ++] = pstAdInfo->m_uiCrtView;
	CJsn[*ppszReq ++] = pstAdInfo->m_uiMinPrice;

	const char* pszRes[] = CM_ADF_TRES;
	const char** ppszRes = &pszRes[0];
	CJsn[*ppszRes ++] = pstAdInfo->m_CAdvId;
	CJsn[*ppszRes ++] = pstAdInfo->m_CDesUrl;
	CJsn[*ppszRes ++] = pstAdInfo->m_CClkUrl;
	CJsn[*ppszRes ++] = pstAdInfo->m_CSupUrl;
	CJsn[*ppszRes ++] = pstAdInfo->m_CExtend;
	CJsn[*ppszRes ++] = pstAdInfo->m_CCategory;
	CJsn[*ppszRes ++] = pstAdInfo->m_uiCode;
	CJsn[*ppszRes ++] = pstAdInfo->m_uiMethod;
	CJsn[*ppszRes ++] = pstAdInfo->m_uiMaxPrice;
	CJsn[*ppszRes ++] = pstAdInfo->m_CCrtId;
	CJsn[*ppszRes ++] = pstAdInfo->m_CCrtTitle;
	CJsn[*ppszRes ++] = pstAdInfo->m_uiCrtType;

	return CJsn.toStyledString();
}

CMADXINFO_S* CCmProJsn::CmProtoDeserialize(char* pszPacket)
{
	Json::Value CJsn;
	Json::Reader CRead;
	CMADXINFO_S* pstInfo = &m_stInfo;

	if (true == CRead.parse(CMSTR_C(pszPacket), CJsn)) {
		const char* pszReq[] = CM_ADF_TREQ;
		const char** ppszReq = &pszReq[0];
		pstInfo->m_uiVer = CJsn[*ppszReq ++].asInt();
		pstInfo->m_uiDsp = CJsn[*ppszReq ++].asInt();
		pstInfo->m_CCtxtid = CJsn[*ppszReq ++].asString();
		pstInfo->m_CIP = CJsn[*ppszReq ++].asString();
		pstInfo->m_CUid = CJsn[*ppszReq ++].asString();
		pstInfo->m_CMac = CJsn[*ppszReq ++].asString();
		pstInfo->m_CGps = CJsn[*ppszReq ++].asString();
		pstInfo->m_CImei = CJsn[*ppszReq ++].asString();
		pstInfo->m_CUa = CJsn[*ppszReq ++].asString();
		pstInfo->m_CDid = CJsn[*ppszReq ++].asString();
		pstInfo->m_CAid = CJsn[*ppszReq ++].asString();
		pstInfo->m_CEid = CJsn[*ppszReq ++].asString();
		pstInfo->m_CCid = CJsn[*ppszReq ++].asString();
		pstInfo->m_CCats = CJsn[*ppszReq ++].asString();
		pstInfo->m_CType = CJsn[*ppszReq ++].asString();
		pstInfo->m_CWidth = CJsn[*ppszReq ++].asString();
		pstInfo->m_CHeight = CJsn[*ppszReq ++].asString();
		pstInfo->m_CScaling = CJsn[*ppszReq ++].asString();
		pstInfo->m_uiCrtView = CJsn[*ppszReq ++].asInt();
		pstInfo->m_uiMinPrice = CJsn[*ppszReq ++].asInt();

		const char* pszRes[] = CM_ADF_TRES;
		const char** ppszRes = &pszRes[0];
		pstInfo->m_CAdvId = CJsn[*ppszRes ++].asString();
		pstInfo->m_CDesUrl = CJsn[*ppszRes ++].asString();
		pstInfo->m_CClkUrl = CJsn[*ppszRes ++].asString();
		pstInfo->m_CSupUrl = CJsn[*ppszRes ++].asString();
		pstInfo->m_CExtend = CJsn[*ppszRes ++].asString();
		pstInfo->m_CCategory = CJsn[*ppszRes ++].asString();
		pstInfo->m_uiCode = CJsn[*ppszRes ++].asInt();
		pstInfo->m_uiMethod = CJsn[*ppszRes ++].asInt();
		pstInfo->m_uiMaxPrice = CJsn[*ppszRes ++].asInt();
		pstInfo->m_CCrtId = CJsn[*ppszRes ++].asString();
		pstInfo->m_CCrtTitle = CJsn[*ppszRes ++].asString();
		pstInfo->m_uiCrtType = CJsn[*ppszRes ++].asInt();
	}
	else {
		pstInfo = NULL;
	}

	return pstInfo;
}
